< previous page page_324 next page >

Page 324
These algorithms are quite interesting, but they are not very useful to a typical Visual Basic programmer. Visual Basic and the Windows operating systems use these techniques to draw lines and circles much more quickly than you could on your own. Rather than implementing your own circle- and line-drawing routines, use those provided by Visual Basic whenever possible.
The sections that follow describe some curves that Visual Basic methods cannot easily draw. You can draw these curves yourself using the Line method.
Parametric Curves
A parametric curve is a curve with X and Y coordinates defined in terms of a third parameter. Usually the parameter is called ''t," and the equations for the coordinates of the points on the curve are written as X(t) and Y(t).
For example, you can write the parametric equations for a circle with radius R centered at the origin as X(t) = R * Cos(t), Y(t) = R * Sin(t). As the parameter t varies between the values 0 and 2 * p, the functions X(t) and Y(t) give the coordinates of the points on the circle.
In Visual Basic, drawing a curve written with parametric equations is easy. Create a variable t and use a While loop to run the variable over the range of parameter values needed to draw the curve. Connect the points on the curve using the Visual Basic Line command.
The DrawCurve subroutine shown in the following code does this. The values cx and cy are chosen to center the drawing in the picture box.
e10a6a0a59f035e870d90cca05b85fce.gif
' Draw the curve on the indicated picture box.
Private Sub DrawCurve(ByVal pic As PictureBox, _
    ByVal start_t As Single, ByVal stop_t As Single, _
    ByVal dt As Single)
Dim cx As Single
Dim cy As Single
Dim t As Single

    cx = pic.ScaleLeft + pic.ScaleWidth / 2
    cy = pic.ScaleTop + pic.ScaleHeight / 2

    pic.Cls
    pic.CurrentX = cx + X(start_t)
    pic.CurrentY = cy + Y(start_t)

    t = start_t + dt
    Do While t < stop_t
        pic.Line -(cx + X(t), cy + Y(t))
        t = t + dt
    Loop

    pic.Line -(cx + X(stop_t), cy + Y(stop_t))
End Sub

 
< previous page page_324 next page >